///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////

package org.kolonitsky.alexey.mvcext.view
{
    import flash.events.Event;

    import mx.core.FlexGlobals;
    import mx.rpc.events.ResultEvent;
    import mx.utils.ArrayUtil;

    import org.kolonitsky.alexey.debug.DebugWrapper;
    import org.kolonitsky.alexey.mvcext.model.GuideStepVO;
    import org.kolonitsky.alexey.mvcext.model.GuideVO;
    import org.kolonitsky.alexey.utils.ArrayUtils;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;

    import spark.components.Group;

    import view.controls.GuideLayer;

    /**
     *
     */
    public class GuideMediator extends Mediator {

        public static const NAME:String = "guildManager";

        public function GuideMediator(elfin:Elfin)
        {
            var guideLayer:GuideLayer = new GuideLayer();
            guideLayer.visible = false;
            guideLayer.includeInLayout = false;
            elfin.addElement(guideLayer);

            scanStartNotification();

            super(NAME, guideLayer);

            DebugWrapper.trace("INFO: " + getMediatorName() + " created.")
        }

        public function get guideLayer():GuideLayer
        {
            return viewComponent as GuideLayer;
        }




        //-------------------------------------------------------------------------
        //
        //  Override mediator logic
        //
        //-------------------------------------------------------------------------

        override public function onRegister():void
        {
            guideLayer.addEventListener(Event.COMPLETE, guideLayer_completeHandler);
            super.onRegister();
        }

        override public function onRemove():void
        {
            super.onRemove();
        }

        override public function listNotificationInterests():Array
        {
            return guideStartNotification;
        }

        override public function handleNotification(notification:INotification):void
        {
            super.handleNotification(notification);
            var noteId:String = notification.getName();

            // switch to next notification
            if (currentGuide
                && currentStep.completeNotification
                && currentStep.completeNotification == noteId)
            {
                nextStep();
                return;
            }

            // start notification
            if (guideStartNotification.indexOf(notification.getName()) != -1)
            {
                var array:Array = ArrayUtils.getItemsByProperty(Config.GUIDES, "startNotification", noteId);
                if (array.length)
                {
                    currentGuide = array[0] as GuideVO;
                    stepIndex = -1;
                    nextStep();
                }
            }
        }




        //-------------------------------------------------------------------------
        //
        //  Private
        //
        //-------------------------------------------------------------------------

        private var guideStartNotification:Array = [];

        private var currentGuide:GuideVO = null;

        private var stepIndex:int = 0;

        private function get currentStep():GuideStepVO
        {
            if (currentGuide && stepIndex < currentGuide.steps.length)
                return currentGuide.steps[stepIndex] as GuideStepVO;
            return null;
        }

        private function nextStep():void
        {
            stepIndex++;
            guideLayer.step = currentStep;
            if (currentGuide && stepIndex >= currentGuide.steps.length)
                currentGuide = null;
        }

        private function scanStartNotification():void
        {
            guideStartNotification = [];
            for each (var guide:GuideVO in Config.GUIDES)
            {
                var index:int = guideStartNotification.indexOf(guide.startNotification);
                if (index == -1)
                {
                    guideStartNotification.push(guide.startNotification);
                    scanCompleteStepNotification(guide.steps);
                }
                else
                {
                    trace("WARNING: Two guide with one startNotification. Guide "
                        + guide.name + " not registered.");
                }
            }
        }

        private function scanCompleteStepNotification(steps:Array /* of GuideStepVO */):void
        {
            for each (var step:GuideStepVO in steps)
            {
                var noteId:String = step.completeNotification;
                if (noteId == "")
                    continue;

                var index:int = guideStartNotification.indexOf(noteId);
                if (index == -1)
                {
                    guideStartNotification.push(noteId);
                }
                else
                {
                    trace("DEBUG: Twi guides completed by one notification: " + noteId);
                }
            }
        }

        private function guideLayer_completeHandler(event:Event):void
        {
            nextStep();
        }

    }
}
